package com.example.guesswho.ui.navigation

import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.example.guesswho.ui.screens.*

@Composable
fun GuessWhoApp(
    navController: NavHostController = rememberNavController(),
    modifier: Modifier = Modifier
) {
    Surface(
        modifier = modifier.fillMaxSize(),
        color = MaterialTheme.colorScheme.background
    ) {
        NavHost(
            navController = navController,
            startDestination = Screen.Welcome.route
        ) {
            composable(Screen.Welcome.route) {
                WelcomeScreen(
                    onStartClick = { navController.navigate(Screen.Match.route) },
                    onRankClick = { navController.navigate(Screen.Rank.route) }
                )
            }
            composable(Screen.Match.route) {
                MatchScreen(
                    onMatchSuccess = { roomId ->
                        navController.navigate(Screen.GamePlay.createRoute(roomId))
                    },
                    onBackClick = { navController.popBackStack() }
                )
            }
            composable(Screen.GamePlay.route) { backStackEntry ->
                val roomId = backStackEntry.arguments?.getString("roomId") ?: ""
                GamePlayScreen(
                    roomId = roomId,
                    onGameEnd = { 
                        navController.navigate(Screen.Rank.route) {
                            popUpTo(Screen.Welcome.route) { inclusive = false }
                        }
                    },
                    onShareClick = { 
                        navController.navigate(Screen.PosterShare.createRoute(roomId))
                    }
                )
            }
            composable(Screen.Rank.route) {
                RankScreen(
                    onBackClick = { navController.popBackStack() },
                    onPlayAgainClick = { navController.navigate(Screen.Match.route) }
                )
            }
            composable(Screen.PosterShare.route) { backStackEntry ->
                val roomId = backStackEntry.arguments?.getString("roomId") ?: ""
                PosterShareScreen(
                    roomId = roomId,
                    onBackClick = { navController.popBackStack() }
                )
            }
        }
    }
}

sealed class Screen(val route: String) {
    object Welcome : Screen("welcome")
    object Match : Screen("match")
    object GamePlay : Screen("game_play/{roomId}") {
        fun createRoute(roomId: String) = "game_play/$roomId"
    }
    object Rank : Screen("rank")
    object PosterShare : Screen("poster_share/{roomId}") {
        fun createRoute(roomId: String) = "poster_share/$roomId"
    }
}